package com.example.Dao;

import com.example.Entity.StudentEntity;
import com.example.Utils.JdbcUtils;

import java.sql.*;
import java.util.ArrayList;

/**
 * @version: java version 23
 * @Author: 韩学新
 * @description:
 * @date: 2025-05-17 下午5:23
 */
public class StudentDao {

    public ArrayList<StudentEntity> getAllStudent() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        ArrayList<StudentEntity> studentEntities = new ArrayList<>();
        try {
            connection = JdbcUtils.getConnection();
            if (connection != null) {
                statement = connection.createStatement();
            }
            if (statement != null) {
                resultSet = statement.executeQuery("select * from student");
            }
            if (resultSet != null) {
                while (resultSet.next()) {
                    Long id = resultSet.getLong("id");
                    String name = resultSet.getString("name");
                    int age = resultSet.getInt("age");
                    StudentEntity studentEntity = new StudentEntity(id, name, age);
                    studentEntities.add(studentEntity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeConnection(resultSet, statement, connection);

        }
        return studentEntities;

//        Connection connection = null;
//        Statement statement = null;
//        ResultSet resultSet = null;
//        ArrayList<StudentEntity> studentEntities = new ArrayList<>();
//        try {
//            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java", "root", "123456");
//            statement = connection.createStatement();
//            resultSet = statement.executeQuery("select * from student");
//            while (resultSet.next()) {
//                Long id = resultSet.getLong("id");
//                String name = resultSet.getString("name");
//                int age = resultSet.getInt("age");
//                StudentEntity studentEntity = new StudentEntity(id, name, age);
//                studentEntities.add(studentEntity);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (resultSet != null) {
//                    resultSet.close();
//                }
//                if (statement != null) {
//                    statement.close();
//                }
//                if (connection != null) {
//                    connection.close();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//        }
//        return studentEntities;


    }

    public int updateStudent(Long id, String name) {
        Statement statement = null;
        Connection connection = null;
        int result = 0;
        try {
            connection = JdbcUtils.getConnection();
            //开启事务
            connection.setAutoCommit(false);
            statement = connection.createStatement();
            result = statement.executeUpdate("update student set name ='" + name + "'" + "where id = " + id);
            //提交事务
            connection.commit();
        } catch (Exception e) {
            try {
                //回滚事务
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            JdbcUtils.closeConnection(statement, connection);
        }
        return result;
    }
}
